<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const shopId = route.params.shopId
const shopName = route.params.shopName || '商家客服'

// 聊天消息列表
const messages = ref([
  {
    id: 1,
    content: '您好，欢迎来到' + shopName + '，有什么可以帮助您的吗？',
    sender: 'shop',
    time: '09:30'
  },
  {
    id: 2,
    content: '请问这件商品有什么颜色可选？',
    sender: 'user',
    time: '09:31'
  },
  {
    id: 3,
    content: '您好，目前有红色、蓝色和黑色三种颜色，您可以根据喜好选择。',
    sender: 'shop',
    time: '09:32'
  }
])

// 新消息输入框
const newMessage = ref('')
const messageInput = ref(null)
const messagesContainer = ref(null)
const isTyping = ref(false)

// 模拟回复选项
const quickReplies = ref([
  '这个商品有货吗？',
  '什么时候能发货？',
  '支持退换货吗？',
  '能便宜一点吗？'
])

// 获取当前时间
const getCurrentTime = () => {
  const now = new Date()
  const hours = now.getHours().toString().padStart(2, '0')
  const minutes = now.getMinutes().toString().padStart(2, '0')
  return `${hours}:${minutes}`
}

// 发送消息
const sendMessage = () => {
  if (!newMessage.value.trim()) return
  
  const message = {
    id: Date.now(),
    content: newMessage.value.trim(),
    sender: 'user',
    time: getCurrentTime()
  }
  
  messages.value.push(message)
  newMessage.value = ''
  
  // 滚动到底部
  scrollToBottom()
  
  // 模拟商家回复
  setTimeout(() => {
    simulateShopReply()
  }, 1000)
}

// 模拟商家回复
const simulateShopReply = () => {
  isTyping.value = true
  
  setTimeout(() => {
    const replies = [
      '您好，我们会尽快为您处理，请稍候。',
      '感谢您的咨询，请问还有其他可以帮助您的吗？',
      '这个问题我需要为您查询一下，请稍等。',
      '好的，我明白了，我会为您安排的。',
      '非常感谢您的支持，祝您购物愉快！'
    ]
    
    const randomReply = replies[Math.floor(Math.random() * replies.length)]
    
    messages.value.push({
      id: Date.now() + 1,
      content: randomReply,
      sender: 'shop',
      time: getCurrentTime()
    })
    
    isTyping.value = false
    scrollToBottom()
  }, 1500)
}

// 点击快捷回复
const useQuickReply = (reply) => {
  newMessage.value = reply
  sendMessage()
}

// 滚动到底部
const scrollToBottom = () => {
  setTimeout(() => {
    if (messagesContainer.value) {
      messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
    }
  }, 100)
}

// 监听键盘事件
const handleKeyPress = (e) => {
  if (e.key === 'Enter' && !e.shiftKey) {
    e.preventDefault()
    sendMessage()
  }
}

// 返回店铺页面
const goBackToShop = () => {
  router.push({ name: 'shop', params: { id: shopId } })
}

onMounted(() => {
  scrollToBottom()
  // 自动获取焦点
  if (messageInput.value) {
    messageInput.value.focus()
  }
})
</script>

<template>
  <div class="chat-view">
    <!-- 聊天头部 -->
    <div class="chat-header">
      <button class="back-btn" @click="goBackToShop">
        <span class="back-icon">←</span>
      </button>
      <div class="shop-info">
        <img src="https://img.alicdn.com/imgextra/i1/O1CN01Z7X5Xs1vX5Xs1vX5_!!6000000006453-2-tps-48-48.png" alt="商家头像" class="shop-avatar" />
        <div class="shop-name">{{ shopName }}</div>
        <div class="online-status">
          <span class="online-indicator"></span>
          <span class="status-text">在线</span>
        </div>
      </div>
      <button class="more-btn">
        <span class="more-icon">⋮</span>
      </button>
    </div>

    <!-- 聊天消息区域 -->
    <div class="chat-messages" ref="messagesContainer">
      <div v-for="message in messages" :key="message.id" class="message-item" :class="message.sender">
        <div class="message-content">
          {{ message.content }}
        </div>
        <div class="message-time">{{ message.time }}</div>
      </div>
      
      <!-- 正在输入提示 -->
      <div v-if="isTyping" class="typing-indicator">
        <span class="typing-dot"></span>
        <span class="typing-dot"></span>
        <span class="typing-dot"></span>
      </div>
    </div>

    <!-- 快捷回复区域 -->
    <div class="quick-replies">
      <button 
        v-for="(reply, index) in quickReplies" 
        :key="index" 
        class="quick-reply-btn"
        @click="useQuickReply(reply)"
      >
        {{ reply }}
      </button>
    </div>

    <!-- 输入区域 -->
    <div class="chat-input-area">
      <textarea 
        ref="messageInput"
        v-model="newMessage" 
        class="message-input" 
        placeholder="请输入消息..."
        rows="1"
        @keypress="handleKeyPress"
      ></textarea>
      <button class="send-btn" @click="sendMessage" :disabled="!newMessage.trim()">
        发送
      </button>
    </div>
  </div>
</template>

<style scoped>
.chat-view {
  background-color: #f5f5f5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  width: 100%;
}

/* 聊天头部样式 */
.chat-header {
  background-color: #fff;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 5px;
  color: #333;
}

.shop-info {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
}

.shop-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

.shop-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.online-status {
  display: flex;
  align-items: center;
  gap: 5px;
}

.online-indicator {
  width: 8px;
  height: 8px;
  background-color: #52c41a;
  border-radius: 50%;
}

.status-text {
  font-size: 12px;
  color: #999;
}

.more-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 5px;
  color: #333;
}

/* 聊天消息区域 */
.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.message-item {
  display: flex;
  flex-direction: column;
  max-width: 70%;
}

.message-item.shop {
  align-self: flex-start;
}

.message-item.user {
  align-self: flex-end;
}

.message-content {
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  word-break: break-word;
}

.message-item.shop .message-content {
  background-color: #fff;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.message-item.user .message-content {
  background-color: #07c160;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message-time {
  font-size: 11px;
  color: #999;
  margin-top: 4px;
  align-self: flex-end;
}

.message-item.shop .message-time {
  align-self: flex-start;
}

/* 正在输入提示 */
.typing-indicator {
  align-self: flex-start;
  background-color: #fff;
  padding: 10px 20px;
  border-radius: 18px;
  border-bottom-left-radius: 4px;
  display: flex;
  gap: 5px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.typing-dot {
  width: 8px;
  height: 8px;
  background-color: #999;
  border-radius: 50%;
  animation: typing 1.4s infinite ease-in-out both;
}

.typing-dot:nth-child(1) {
  animation-delay: -0.32s;
}

.typing-dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes typing {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 快捷回复区域 */
.quick-replies {
  background-color: #fff;
  padding: 15px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  gap: 10px;
  overflow-x: auto;
  flex-wrap: nowrap;
}

.quick-reply-btn {
  padding: 8px 16px;
  background-color: #f5f5f5;
  border: 1px solid #e0e0e0;
  border-radius: 18px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.3s;
}

.quick-reply-btn:hover {
  background-color: #f0f0f0;
  border-color: #d0d0d0;
}

/* 输入区域 */
.chat-input-area {
  background-color: #fff;
  padding: 15px 20px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.message-input {
  flex: 1;
  min-height: 36px;
  max-height: 120px;
  padding: 8px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 18px;
  font-size: 14px;
  resize: none;
  outline: none;
  transition: border-color 0.3s;
  font-family: inherit;
}

.message-input:focus {
  border-color: #07c160;
}

.send-btn {
  padding: 8px 20px;
  background-color: #07c160;
  color: #fff;
  border: none;
  border-radius: 18px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  white-space: nowrap;
}

.send-btn:hover:not(:disabled) {
  background-color: #06ad56;
}

.send-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

/* 滚动条样式 */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .message-item {
    max-width: 85%;
  }
  
  .quick-replies {
    padding: 10px 15px;
  }
  
  .chat-input-area {
    padding: 10px 15px;
  }
}
</style>